package com.yx.controller;

import com.yx.common.R;
import com.yx.feign.api.UserService;
import com.yx.vo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * @Classname UserController
 * @Date 2021/07/06 12:04
 * @Author by yx
 * @Description TODO:
 */
@Controller
@RequestMapping("/userController")
public class UserController {

    @Autowired
    private UserService userService;

    @GetMapping("/{page}.html")
    public String toPage(@PathVariable String page) {
        // 此时page就是addUser，userList,xxxx
        return "user/" + page; // user/addUser,user/userList
    }

    @PostMapping("/addUser")
    @ResponseBody
    public R addUser(User user) { // 这里接收的是VO，主要是表单传递过来
       Boolean trager =  userService.addUser(user);
        return R.write(trager);
    }

    @GetMapping("/getUserList")
    public String getUserList(ModelMap modelMap){
        List<User> userList = userService.getUserList();
        modelMap.put("userList",userList);
        return "user/userList";
    }

    @PostMapping("/queryUserList")
    public String queryUserList(User user,ModelMap modelMap){
        List<User> userList = userService.queryUserList(user);
        modelMap.put("userList",userList);
        modelMap.put("user", user);
        return "user/userList";
    }

    @GetMapping("/updateUser/{id}")
    public String updateUser(@PathVariable("id") Integer id,ModelMap modelMap){
        User user = userService.getUser(id);
        modelMap.put("user", user);
        return "user/editUser";
    }

    @PostMapping("/updateUser")
    @ResponseBody
    public R updateUser(User user) { // 这里接收的是VO，主要是表单传递过来
        Boolean trager =  userService.updateUser(user);
        return R.write(trager);
    }

    @PostMapping("/deleteUser")
    @ResponseBody
    public R deleteUser(int[] ids){
        return R.write(userService.deleteUsers(ids));
    }

    @GetMapping("/deleteUserById/{id}")
    @ResponseBody
    public R deleteUserById(@PathVariable("id") Integer id){
        Boolean targer = userService.deleteUserById(id);
        return R.write(targer);
    }
}

